package com.jzlin.funhub.admin.service.article;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzlin.funhub.admin.mapper.ArticleCommentMapper;
import com.jzlin.funhub.admin.model.dto.ArticleCommentPageDTO;
import com.jzlin.funhub.admin.model.entity.ArticleComment;
import com.jzlin.funhub.admin.model.req.ArticleCommentPageReq;
import com.jzlin.funhub.admin.model.req.ArticleCommentPublishReq;
import com.jzlin.funhub.admin.service.system.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 文章评论表 服务实现类
 * </p>
 *
 * @author jzlin
 * @since 2025-07-27
 */
@Service
public class ArticleCommentService extends ServiceImpl<ArticleCommentMapper, ArticleComment> {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 发表评论
     *
     * @param req
     */
    @Transactional
    public void publishArticleComment(ArticleCommentPublishReq req) {
        StpUtil.checkLogin();
        ArticleComment articleComment = new ArticleComment();
        articleComment.setContent(req.getContent());
        articleComment.setArticleId(req.getArticleId());

        if (this.save(articleComment)) {
            articleService.increaseArticleCommentCount(req.getArticleId());
            sysUserService.increaseArticleCommentCount(StpUtil.getLoginIdAsLong());
        }
    }

    /**
     * 分页查询评论
     *
     * @param req
     */
    public Page<ArticleCommentPageDTO> queryPage(ArticleCommentPageReq req) {
        Page<ArticleComment> page = new Page<>(req.getCurrent(), req.getSize());

        return this.baseMapper.queryPage(page, req.getArticleId());
    }
}
